/**
 * 学生入所
 * 
 * /student/chooisetopic
 */

const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const Topics = require('../../../models/researchTopics')
const Topic_Student = require('../../../models/researchTopicStudents')
const MySQLManager = require('../../../utils/MySQLManager');
const sequelize = MySQLManager


const chooise_topic = async function(req, res) {    
    try {

        if (!req.body.topic_id) {
            $.sendMessage(res, 1015, '参数错误')
            return
        }

        // 根据科研题目id找到年度id
        var topic = await Topics.findOne({
            where:{
                id: req.body.topic_id
            }
        })

        if( topic == null) {
            $.sendMessage(res, 1015, '无效科研题目id')
            return
        }

        // 如果题目被锁定，则不能报名 topic_status 1 解锁 0 锁定
        if (topic.dataValues.topic_status != '1') {
            $.sendMessage(res, 1015, '题目被老师锁定的，不能报名哦')
            return
        }

        var year_id = topic.dataValues.years

        // 从该年度找到学生
        var student = await Topic_Student.findOne({
            where:{
                student_year: year_id,
                student_user_name: req.user_name
            }
        })
        if (student == null) {
            $.sendMessage(res, 1015, '您不在该年度里，无法报名该题目')
            return
        }

        // 状态 0已报名 1未报名 2老师确认
        // 查看学生的当前状态，已经报名别的课题或者老师已经确认则不能继续报名
        if (student.dataValues.student_status != '1') {
            $.sendMessage(res, 1015, '您已经报名了某个课题，不能再次报名')
            return
        }

        // 以上逻辑都通过，进行报名
        // 报名成功之后更新当前报名人数哦

         // 事务
         return sequelize.transaction(function (t) {

            return Topic_Student.update(
            {
                topic_id: req.body.topic_id,
                student_status: '0',
                topic_name: topic.dataValues.topic_name,
                teacher_id: topic.dataValues.teacher_user_name,
                teacher_fullname: topic.dataValues.teacher_fullname,
                topic_name: topic.dataValues.topic_name,

            },
            {
                where: {
                    student_user_name: req.user_name + ''
                }
            }
            , 
            {transaction: t}).then(function (user) {
            return Topics.findOne(
                {
                    where: {
                        id: req.body.topic_id
                    }
                }
                , {transaction: t})
            }).then( topic => {

               return topic.increment('current_student'
               ,{transaction: t}).then( () => {
               })
            })
        })
        // 请求完整完成
        .then(function (result) {
            res.json({
                code: 200,
                message: '报名成功',
                result: result
            })
            utils.create_a_log(req, '学生入所', '报名题目')
            return
        })
        // 处理sequalize异常
        .catch(function (err) {
            res.json({
                code: 1017,
                message: '报名失败，请联系管理员',
                err: 'err' + err
            })
            return
        })
    }

    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '学生入所', '报名题目', '失败')
        return
    }
}

module.exports = chooise_topic